#include "GPComSettingWidget.h"
#include "ui_GPComSettingWidget.h"
#include <QSerialPortInfo>

#include <QIcon>
#define ICON_COM_ENABLE ":/icon/icon/com-enable.svg"
#define ICON_COM_DISABLE ":/icon/icon/com-disable.svg"
GPComSettingWidget::GPComSettingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GPComSettingWidget)
{
    ui->setupUi(this);
    QList<QSerialPortInfo> comInfos = QSerialPortInfo::availablePorts();
    for(const QSerialPortInfo& info : comInfos)
    {
        QString name = info.portName();
        if(info.isBusy())
        {
            ui->comboBoxComName->addItem(QIcon(ICON_COM_DISABLE),name);
        }
        else
        {
            ui->comboBoxComName->addItem(QIcon(ICON_COM_ENABLE),name);
        }
    }
    //波特率设置
    ui->comboBoxBaudRate->addItem("1200",QSerialPort::Baud1200);
    ui->comboBoxBaudRate->addItem("2400",QSerialPort::Baud2400);
    ui->comboBoxBaudRate->addItem("4800",QSerialPort::Baud4800);
    ui->comboBoxBaudRate->addItem("9600",QSerialPort::Baud9600);
    ui->comboBoxBaudRate->addItem("19200",QSerialPort::Baud19200);
    ui->comboBoxBaudRate->addItem("38400",QSerialPort::Baud38400);
    ui->comboBoxBaudRate->addItem("57600",QSerialPort::Baud57600);
    ui->comboBoxBaudRate->addItem("115200",QSerialPort::Baud115200);
    ui->comboBoxBaudRate->setCurrentIndex(3);
    //校验
    ui->comboBoxParity->addItem(tr("None"),QSerialPort::NoParity);
    ui->comboBoxParity->addItem(tr("Even"),QSerialPort::EvenParity);
    ui->comboBoxParity->addItem(tr("Odd"),QSerialPort::OddParity);
    ui->comboBoxParity->addItem(tr("Space"),QSerialPort::SpaceParity);
    ui->comboBoxParity->addItem(tr("Mark"),QSerialPort::MarkParity);
    ui->comboBoxParity->setCurrentIndex(0);
    //数据位
    ui->comboBoxDataBits->addItem("5",QSerialPort::Data5);
    ui->comboBoxDataBits->addItem("6",QSerialPort::Data6);
    ui->comboBoxDataBits->addItem("7",QSerialPort::Data7);
    ui->comboBoxDataBits->addItem("8",QSerialPort::Data8);
    ui->comboBoxDataBits->setCurrentIndex(3);
    //停止位
    ui->comboBoxStopBits->addItem("1",QSerialPort::OneStop);
    ui->comboBoxStopBits->addItem("1.5",QSerialPort::OneAndHalfStop);
    ui->comboBoxStopBits->addItem("2",QSerialPort::TwoStop);
    ui->comboBoxStopBits->setCurrentIndex(0);

}

GPComSettingWidget::~GPComSettingWidget()
{
    delete ui;
}

QString GPComSettingWidget::getPortName() const
{
    return ui->comboBoxComName->currentText();
}

void GPComSettingWidget::setBaudRate(QSerialPort::BaudRate v)
{
    int index = ui->comboBoxBaudRate->findData(v);
    ui->comboBoxBaudRate->setCurrentIndex(index);
}

QSerialPort::BaudRate GPComSettingWidget::getBaudRate() const
{
    QVariant d = ui->comboBoxBaudRate->currentData();
    if(d.isValid())
    {
        return d.value<QSerialPort::BaudRate>();
    }
    return  QSerialPort::UnknownBaud;
}

void GPComSettingWidget::setParity(QSerialPort::Parity v)
{
    int index = ui->comboBoxParity->findData(v);
    ui->comboBoxParity->setCurrentIndex(index);
}

QSerialPort::Parity GPComSettingWidget::getParity() const
{
    QVariant d = ui->comboBoxParity->currentData();
    if(d.isValid())
    {
        return d.value<QSerialPort::Parity>();
    }
    return  QSerialPort::UnknownParity;
}

void GPComSettingWidget::setDataBits(QSerialPort::DataBits v)
{
    int index = ui->comboBoxDataBits->findData(v);
    ui->comboBoxDataBits->setCurrentIndex(index);
}

QSerialPort::DataBits GPComSettingWidget::getDataBits() const
{
    QVariant d = ui->comboBoxDataBits->currentData();
    if(d.isValid())
    {
        return d.value<QSerialPort::DataBits>();
    }
    return  QSerialPort::UnknownDataBits;
}

void GPComSettingWidget::setStopBits(QSerialPort::StopBits v)
{
    int index = ui->comboBoxStopBits->findData(v);
    ui->comboBoxStopBits->setCurrentIndex(index);
}

QSerialPort::StopBits GPComSettingWidget::getStopBits() const
{
    QVariant d = ui->comboBoxStopBits->currentData();
    if(d.isValid())
    {
        return d.value<QSerialPort::StopBits>();
    }
    return  QSerialPort::UnknownStopBits;
}
